<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/dayjs/1.10.6/dayjs.min.js"></script>
</head>
<body>
    <div id="root">
        <h2>显示格式化后的时间{{fmtTime}}</h2><br>
        <!-- methods实现 -->
        <h3>现在是：{{getFmtTime()}}</h3><br>
        <!-- 过滤器实现 -->
        <h3>现在是：{{time | timeFormater | mySlice}}</h3>
    </div>
    <div id="root2">
        <h2>{{msg | mySlice}}</h2>
    </div>

    <script>
        Vue.filter('mySlice', function(value){
            return value.slice(0, 5)
        })
        const vm = new Vue({
            el: '#root',
            data: {
                time: 1621561377603
            },
            computed: {
                fmtTime() {
                    return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')
                }
            },
            methods: {
                getFmtTime() {
                    return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')
                }
            },
            filters: {
                timeFormater(value, str='YYYY年MM月DD日') {
                    return dayjs(value).format('YYYY年MM月DD日 HH:mm:ss')
                },
                mySlice(value) {
                    return value.slice(0, 4);
                }
            }
        })

        new Vue({
            el: '#root2',
            data: {
                msg: 'hello,atguigu'
            }
        })
    </script>
</body>
</html>